<?php
namespace Qy {
    /**
    * 企业微信部门管理类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8
    */
    class Department {
        private $common;
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 创建部门
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8
        * 
        * @param Array $ary
        * @param AccessToken $a_token
        */
        public function createDepartment($ary=array(),$a_token='') {
            if (empty($ary)) return false;
            if (!isset($ary['name'])||!isset($ary['parentid'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        /**
        * 更新部门信息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8
        * 
        * @param Array $ary
        * @param AccessToken $a_token
        */
        public function updateDepartment($ary=array(),$a_token='') {
            if (empty($ary)) return false;
            if (!isset($ary['id'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 删除部门
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8
        * 
        * @param mixed $id
        * @param mixed $a_token
        */
        public function deleteDepartment($id,$a_token='') {
            if (empty($id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={$a_token}&id={$id}";
            return $this->common->httpRequest($url);
        }
        /**
        * 获取部门列表
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8
        * 
        * @param mixed $id
        * @param mixed $a_token
        */
        public function getDepartmentList($id='',$a_token='') {
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            if (empty($id)) {
                $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={$a_token}";
            }else{
                $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={$a_token}&id={$id}";
            }
            return $this->common->httpRequest($url);
        }
    }
}
?>
